package com.lighting.shortlink.project.config;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.lighting.shortlink.project.common.constant.RedisKeyConstant.SHORT_LINK_CREATE_BLOOM_KEY;

/**
 * 布隆过滤器配置
 * @author : lighting
 */
@Configuration
public class RBloomFilterConfiguration {
    /**
     * 防止短链接创建查询数据库的布隆过滤器
     */
    @Bean
    public RBloomFilter<String> shortUriCreateCachePenetrationBloomFilter(RedissonClient redissonClient)
    {
//        使用 RedissonClient 的布隆过滤器
        RBloomFilter<String> cachePenetrationBloomFilter = redissonClient.getBloomFilter(SHORT_LINK_CREATE_BLOOM_KEY);
//      初始化布隆过滤器               预估布隆过滤器存储的元素个数     预期的错误率
        cachePenetrationBloomFilter.tryInit(100_000_000L,0.001);
        return cachePenetrationBloomFilter;
    }
}


